﻿@{
    ViewData["Title"] = "广告管理-广告位管理";
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.CurrentMenuP = "Advert";
    ViewBag.CurrentMenu = "Advert/List";
}
@using Yuebon.WebApp.Commons;
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-body">
                <div id="toolbar" class="btn-group">
                    @if (@ViewBag.AuthorizeKey.CanInsert)
                    {
                        <button id="btn_add" type="button" class="btn btn-primary" data-toggle="modal" onclick="ShowEditOrViewDialog()" data-whatever="新增">
                            <i class="fas fa-plus" aria-hidden="true"></i>新增
                        </button>
                    }
                    @if (@ViewBag.AuthorizeKey.CanUpdate)
                    {
                        <button id="btn_edit" type="button" class="btn btn-info" data-toggle="modal" onclick="ShowEditOrViewDialog('edit')" data-whatever="修改">
                            <i class="fas fa-edit" aria-hidden="true"></i>修改
                        </button>
                    }
                    @if (@ViewBag.AuthorizeKey.CanUpdateEnable)
                    {
                        <button type="button" class="btn btn-secondary" onclick="EnableByIds('0','/CMS/Advert')"><i class=" fa fa-stop-circle"></i>禁用</button>
                        <button type="button" class="btn btn-success" onclick="EnableByIds('1','/CMS/Advert')"><i class=" fa fa-play-circle"></i>启用</button>
                    }
                    @if (@ViewBag.AuthorizeKey.CanDeleteSoft)
                    {
                        <button id="btn_delete" type="button" class="btn btn-warning" onclick="DeleteByIds('/CMS/Advert/DeleteSoftByIds')">
                            <i class="fas fa-trash-alt" aria-hidden="true"></i>软删除
                        </button>
                    }
                    @if (@ViewBag.AuthorizeKey.CanDelete)
                    {
                        <button id="btn_delete" type="button" class="btn btn-danger" onclick="DeleteByIds('/CMS/Advert/DeleteByIds')">
                            <i class="fas fa-trash-alt" aria-hidden="true"></i>删除
                        </button>
                    }

                </div>
                <table id="tbGrid"></table>
            </div>
            <!-- /.card-body -->
        </div>
        <!-- /.card -->
    </div>
</div>

<link rel="stylesheet" href="~/lib/jquery-treegrid/css/jquery.treegrid.css">
<script src="~/lib/bootstrap-table/extensions/treegrid/bootstrap-table-treegrid.min.js"></script>
<script src="~/lib/jquery-treegrid/js/jquery.treegrid.js"></script>
<!-- page script -->
<script>
    $(function () {
        //1.初始化Table
        var oTable = new TableInit();
        oTable.Init();
        InitDictItem();
    });

    //加载数据表格
    var $table = $('#tbGrid');
    var TableInit = function () {
        var oTableInit = new Object();
        var keywords = "";
        //初始化Table
        oTableInit.Init = function () {
            $table.bootstrapTable({
                url: '/CMS/Advert/GetAll',         //请求后台的URL（*）
                method: 'get',                      //请求方式（*）
                toolbar: '#toolbar',                //工具按钮用哪个容器
                striped: true,
                sidePagination: 'server',
                sortable: false,
                queryParams: oTableInit.queryParams,//传递参数（*）
                //search: true,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
                //strictSearch: true,
                showColumns: true,                  //是否显示所有的列
                showRefresh: true,                  //是否显示刷新按钮
                minimumCountColumns: 2,             //最少允许的列数
                clickToSelect: true,                //是否启用点击选中行
                search: true,
                searchOnEnterKey: true,
                searchText: keywords,
                idField: 'Id',
                columns: [
                    {
                        checkbox: true
                    }, {
                        field: 'Title',
                        title: '名称',
                        sortable: true,
                        searchable: true,
                        formatter: function (value, row, index) {
                            return value+"(宽"+row.ViewWidth+"px X 高"+row.ViewHeight+"px)"
                        },
                    }, {
                        field: 'Type',
                        title: '类型',
                        sortable: true,
                        searchable: true,
                        formatter: function (value, row, index) {
                            var typeStr = "";
                            if (value == "text") {
                                typeStr = "文本";
                            } else if (value == "image") {
                                typeStr = "图片";
                            }else if (value == "carousel") {
                                typeStr = "幻灯片";
                            }else if (value == "gif") {
                                typeStr = "动画";
                            }else if (value == "video") {
                                typeStr = "FLV视频";
                            }else if (value == "code") {
                                typeStr = "代码";
                            }
                            return typeStr
                        },
                    }, {
                        field: 'EnCode',
                        title: '广告位识别码',
                        sortable: true,
                        searchable: true,
                    },{
                        field: 'ViewNum',
                        title: '数量',
                        sortable: true,
                        searchable: true,
                    }, {
                        field: 'Target',
                        title: '链接打开方式',
                        sortable: true,
                        searchable: true,
                        formatter: function (value, row, index) {
                            var Rul = "";
                            if (value == "_blank") {
                                Rul = "新页面";
                            } else if (value == "_self") {
                                Rul = "当前页面";
                            }
                            return Rul
                        },
                    },  {
                        field: 'CreatorTime',
                        title: '创建时间',
                        sortable: true,
                        align: "center",
                    },
                    {
                        field: 'EnabledMark',
                        title: '状态',
                        sortable: true,
                        align: "center",
                        formatter: function (value, row, index) {
                            return value ? '<i class=\"fas fa-toggle-on\" style=\"color:#28a745\">启用</i>' : "<i class=\"fas fa-toggle-off\">禁用</i>"
                        }
                    },
                    {
                        field: 'DeleteMark',
                        title: '删除',
                        sortable: true,
                        align: "center",
                        formatter: function (value, row, index) {
                            return value ? '<i class=\"fas fa-toggle-on\" style=\"color:#ff0000\">是</i>' : "<i class=\"fas fa-toggle-off\">否</i>";
                        }
                    }
                ],
                treeShowField: 'Title',
                parentIdField: 'ParentId',
                onLoadSuccess: function (data) {
                    $table.treegrid({
                        treeColumn: 1,
                        onChange: function () {
                            $table.bootstrapTable('resetWidth');
                        }
                    });
                },
                onSearch: function (text) {
                    keywords = text;
                    return true;
                },
            });
        };

        //得到查询的参数
        oTableInit.queryParams = function (params) {
            var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                limit: params.limit,   //页面大小
                offset: params.offset,  //页码
                order: params.order,
                sort: params.sort,
                search: $("input[ name='search_text' ] ").val(), //定义传输的搜索参数
            };
            return temp;
        };
        return oTableInit;
    };


    //初始化数据字典
    function InitDictItem() {

    }
    //新增、修改或查看明细信息（绑定显示数据）
    function ShowEditOrViewDialog(view) {
        if (view != null) {
            //首先取出来用户选择的数据的ID
            var rows = $table.bootstrapTable('getSelections');
            //首先取出来值判断用户只能选择一个
            if (rows.length == 0) {
                toastr.warning("请选择一条记录");
                return;
            }
            else if (rows.length > 1) {
                toastr.warning("每次只能修改/查看一条记录，你已经选择了<font color='red'  size='6'>" + rows.length + "</font>条");
                return;
            }
        }
        if (view == null) {
            //处理修改的信息
            window.location.href = "/CMS/Advert/Edit"
        } else if (view == "edit") {
            //处理修改的信息
            //$('#addFormModal').modal('show');  //手动显示
            //$('#addFormModal').find('.modal-title').text("编辑");
            ////绑定修改详细信息的方法
            //BindEditInfo();
            var ID = $table.bootstrapTable('getSelections')[0].Id;  //获取到了用用户选择的ID
            window.location.href = "/CMS/Advert/Edit/?id=" + ID;
        }
    }
</script>